package app;

import java.awt.Choice;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;

import javax.swing.JLabel;
import javax.swing.JToolBar;

import addons.GestioneErrori;

/**
 * 
 * Classe per organizzare la GUI del SecondMenu
 * 
 * @author TeamVulcan
 * 
 */
public class PlayerPane extends JToolBar {
	private JLabel pname;
	private Choice pability;
	private Choice pteam;
	private static int count;

	public PlayerPane() {
		super();
		this.setEnabled(false);
	}


	public void makePlayerPane() {

		this.pname = new JLabel();
		this.pability = getChoiceFromFile();
		this.pteam = new Choice();
		pteam.add("Team1");
		pteam.add("Team2");
		add(pname);
		add(pability);
		add(pteam);
		this.setEnabled(false);
	}

	public void MakeNPC() {
		pname = new JLabel();
		pteam = new Choice();
		pname.setText("NPC " + Integer.toString(count + 1));
		pteam.add("Team1");
		pteam.add("Team2");
		pteam.select(1);
		pability = getChoiceFromFile();
		pability.select(0);
		count++;
		add(pname);
		add(pability);
		add(pteam);

	}

	public static void resetNPCcount() {
		count = 0;
	}

	public JLabel getPname() {
		return pname;
	}

	public void setPname(JLabel pname) {
		this.pname = pname;
	}

	public Choice getPability() {
		return pability;
	}

	public void setPability(Choice pability) {
		this.pability = pability;
	}

	public Choice getPteam() {
		return pteam;
	}

	public void setPteam(Choice pteam) {
		this.pteam = pteam;
	}

	private Choice getChoiceFromFile() {
		BufferedReader reader = null;
		Choice choice = new Choice();
		String buff = null;
		try {
			reader = new BufferedReader(new java.io.FileReader((new File(
					"ability.txt"))));
		} catch (Exception e) {
			GestioneErrori.getGestore().reportError(0); // 0:file inesistente
		}
		do {
			try {
				buff = reader.readLine();
			} catch (Exception e) {
				GestioneErrori.getGestore().reportError(1); // 1:lettura file
				// falita
			}
			if (buff != null)
				choice.add(buff);
		} while (buff != null);
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
				GestioneErrori.getGestore().reportError(3); // chiusura file
				// falita
			}
		}
		return choice;
	}

}
